<?php
/**
 * 商品类型
 *
 * @author karl <karl.tang@aiyuangong.com>
 * @link http://www.aiyuangong.com
 */
namespace app\admin\controller\type;

use app\admin\controller\system\Item;
use think\Loader;
use think\Db;
use think\Exception;
use think\exception\HttpException;

class GoodsType extends Item
{

    protected $tag = 'goods_type';
    
    // 层级
    protected $level = 1;
    
    // 黑名单方法，禁止访问某些方法
    protected static $blacklist = [];
    
    // 列表字段
    protected $row = [
        'id',
        'name',
        'tag',
        'remark',
        'sort'
    ];

    protected $title = [
        'name' => '类型名称',
        'tag' => '标识',
        'remark' => '备注',
        'sort' => '排序'
    ];
    
    // 列表显示的按钮
    protected $extend_list_btns = [
        [
            'name' => '关联品牌',
            'url' => 'relateBrand',
            'type' => 'layer_open',
            'icon' => '&#xe692;'
        ],
        [
            'name' => '关联规格',
            'url' => 'relateSpec',
            'type' => 'layer_open',
            'icon' => '&#xe627;'
        ]
    ];

    /**
     * 关联品牌
     * 
     * @return mixed
     */
    public function relateBrand()
    {
        $controller = $this->getRealController();
        $module = $this->request->module();
        
        if ($this->request->isAjax()) {
            // 更新
            $typeId = $this->request->post('id/i');
            $brandList = $this->request->post('brand/a');
            if (! $typeId) {
                return ajax_return_adv_error("缺少参数ID");
            }
            $brandData = [];
            if (isset($brandList) && ! empty($brandList)) {
                foreach ($brandList as $key => $val) {
                    $brandData[] = [
                        'type_id' => $typeId,
                        'brand_id' => $val
                    ];
                }
            }
            // 更新数据
            Db::startTrans();
            try {
                $model = Loader::model('goods_type_brand');
                $ret = $model->delBrand([
                    'type_id' => $typeId
                ]);
                $ret = $model->insertAll($brandData);
                // 提交事务
                Db::commit();
                return ajax_return_adv("关联成功");
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                
                return ajax_return_adv_error($e->getMessage());
            }
        } else {
            // 编辑
            $id = $this->request->param('id');
            if (! $id) {
                throw new Exception("缺少参数ID");
            }
            // 品牌列表
            $type = \Fields::getById('goods_type', $id);
            if (empty($type)) {
                throw new Exception("无该类型，ID：" . $id);
            }
            $brandList = Db::name("goods_brand")->select();
            $this->view->assign('brand_list', $brandList);
            $this->view->assign("type", $type);
            // 类型品牌
            $typeBrand = Db::name('goods_type_brand')->field('brand_id')
                ->where('type_id', 'eq', $id)
                ->select();
            if (isset($typeBrand) && ! empty($typeBrand))
                $typeBrand = array_map('reset', $typeBrand);
            $this->view->assign('type_brand', $typeBrand);
            
            return $this->view->fetch();
        }
    }

    /**
     * 关联规格
     * 
     * @return mixed
     */
    public function relateSpec()
    {
        $controller = $this->getRealController();
        $module = $this->request->module();
        
        if ($this->request->isAjax()) {
            // 更新
            $typeId = $this->request->post('id/i');
            $specList = $this->request->post('spec/a');
            if (! $typeId) {
                return ajax_return_adv_error("缺少参数ID");
            }
            $specData = [];
            if (isset($specList) && ! empty($specList)) {
                foreach ($specList as $key => $val) {
                    $specData[] = [
                        'type_id' => $typeId,
                        'spec_id' => $val
                    ];
                }
            }
            
            // 更新数据
            Db::startTrans();
            try {
                $model = Loader::model('goods_type_spec');
                $ret = $model->delSpec([
                    'type_id' => $typeId
                ]);
                $ret = $model->insertAll($specData);
                // 提交事务
                Db::commit();
                
                return ajax_return_adv("编辑成功");
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                
                return ajax_return_adv_error($e->getMessage());
            }
        } else {
            // 编辑
            $id = $this->request->param('id');
            if (! $id) {
                throw new Exception("缺少参数ID");
            }
            $type = \Fields::getById('goods_type', $id);
            if (! $type) {
                throw new HttpException(404, '该记录不存在');
            }
            // 规格和品牌列表
            $specList = \Fields::getById('goods_spec');
            $this->view->assign('spec_list', $specList);
            $this->view->assign("type", $type);
            // 类型品牌
            $typeSpec = Db::name('goods_type_spec')->field('spec_id')
                ->where('type_id', 'eq', $id)
                ->select();
            if (isset($typeSpec) && ! empty($typeSpec))
                $typeSpec = array_map('reset', $typeSpec);
            $this->view->assign('type_spec', $typeSpec);
            
            return $this->view->fetch();
        }
    }
}